package com.sys.web.UI;

import java.io.File;
import java.io.IOException;
import java.sql.SQLException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.sys.dao.FileDao;
import com.sys.dao.UserDao;
import com.sys.dao.impl.FileDaoImpl;
import com.sys.dao.impl.UploadDaoImpl;
import com.sys.dao.impl.UserDaoImpl;
import com.sys.domain.Upload;
import com.sys.domain.User;

@WebServlet("/adminUI")
public class AdminUI extends HttpServlet {

	private static final long serialVersionUID = 1L;

	public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String user = (String) request.getSession().getAttribute("user");
		if(user == null || !user.equals("admin")) {
			response.sendRedirect(request.getContextPath()+File.separator+"homeUI");
			return;
		}
		List<Upload> notAppro = null;
		try {
			notAppro = new UploadDaoImpl().findAllNotAppro();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		FileDao fDao = new FileDaoImpl();
		List<com.sys.domain.File> files = null;
		try {
			files = fDao.allFiles();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		UserDao userDao = new UserDaoImpl();
		List<User> users = null;
		try {
			users = userDao.allUsers();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		request.setAttribute("users", users);
		request.setAttribute("fileArr", files);
		request.setAttribute("notAppro", notAppro);
		request.getRequestDispatcher("/WEB-INF/zadminpages/adminHome.jsp").forward(request, response);
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doGet(request, response);
	}
}